
package com.itheima.reggie.contraller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@RestController
@Slf4j
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;


    @Autowired
    private CategoryService categoryService;


    //新增菜品
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    //分页条件查询
    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page,Integer pageSize,String name){
        //构造分页对象
        Page<Dish> pageInfo1  = new Page<>(page,pageSize);
        //构造条件对象
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);//StringUtils选apache.commons.lang
        queryWrapper.orderByAsc("sort").orderByDesc("update_time");
        //调用service方法
        dishService.page(pageInfo1,queryWrapper);


        //对Page<Dish> pageInfo 做数据处理
        Page<DishDto> pageInfo2 = new Page<>(page,pageSize);

        //1.将page1中的数据考贝到page2中（分为两个阶段：List.Total）
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        //2.将page1中的List<Dish>转为 List<DishDto>
        List<DishDto> dishDtoList = new ArrayList<>();

        //遍历page1中的list集合
        for (Dish dish : pageInfo1.getRecords()) {
            //将Dish转变为DishDto
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            //T0D0 处理分类名称
            Category category=categoryService.getById(dishDto.getCategoryId());
            dishDto.setCategoryName(category.getName());
            dishDtoList.add(dishDto);
        }

        pageInfo2.setRecords(dishDtoList);
        return R.success(pageInfo2);



    }


    //根据id查询菜品信息和对应的口味信息
    @GetMapping("{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }



    //修改菜品
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品信息成功");
    }



   //根据分类id查询菜品列表(套餐管理)
    @GetMapping("list")
    public R<List<DishDto>> list(Dish dish){
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Dish::getStatus, 1);
        queryWrapper1.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        queryWrapper1.eq(StringUtils.isNotBlank(dish.getName()), Dish::getName, dish.getName());
        queryWrapper1.orderByAsc(Dish::getSort).orderByAsc(Dish::getUpdateTime);
        //查询列表
        List<Dish> dishList = dishService.list(queryWrapper1);

        //将每个Dish对象转换为DishDto
        List<DishDto> dishDtoList = dishList.stream().map(item -> {
            DishDto dishDto = new DishDto();
            //基本属性拷贝
            BeanUtils.copyProperties(item, dishDto);
            //根据菜品id查询对应的口味集合
            LambdaQueryWrapper<DishFlavor> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(DishFlavor::getDishId, item.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper2);
            //设置菜品口味的数据
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtoList);




       /* //Select * from dish where category_Id? and status=1
        QueryWrapper<Dish> queryWrapper=new QueryWrapper<>();

        //查询条件
        queryWrapper.eq(categoryId!=null,"category_id",categoryId);
        queryWrapper.eq("status",1);

        //排序查询
        queryWrapper.orderByAsc("sort").orderByDesc("update_time");

        //调用业务层
        List<Dish> dishList = dishService.list(queryWrapper);
        return R.success(dishList);
*/





         /*
     //根据条件查询对应的菜品数据

    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null ,Dish::getCategoryId,dish.getCategoryId());
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(Dish::getStatus,1);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(queryWrapper);
        return R.success(list);
    }*/

    }




}
